var Comments = Class.create();

Object.extend(Comments, {
  email_regex: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
  
  validate: function(form) { 
    errors = ""
  
    inputs = Form.getElements(form); 
    inputs.each(function(input) {
      switch(input.name) {
        case "name":
          if (input.value == "")
            errors += "Please enter your name.<br/>";
          break;
        case "email":
          if ((input.value != "") && (!Comments.email_regex.test(input.value))) 
            errors += "Please enter a valid email.<br/>";
          break;
        case "body":
          if (input.value == "")
            errors += "Please enter a comment.<br/>";
          break;
      }
    });
    
    if (errors == "")
      return true;
    else {
      Submit.updateErrors(errors);
      return false;
    }
  },
  
  hideFirstCommentFromBar: function() {
    comments = $('bar-comments-list').childElements();
    
    if (comments.size() >= 8) {
      comments[0].remove();
    }
  },
  
  lightboxLoad: function(photo_id) {
    $('lightbox-comments-list').update('');
    new Ajax.Request(
      '/comments/list?commentable_type=Photo&commentable_id=' + photo_id
    );
    $('lightbox-comments-formholder').appendChild($('lightbox-comment-form'));
    $('lightbox-commentable-id').value = photo_id;
  },
  
  handleError: function(request) {
    if (request.status == 401) {
      Comments.showSpamTest(Submit.currentForm);
    }
    else
      Submit.updateErrors('Sorry, something went wrong.');
  },
  
  showSpamTest: function(form) {
    if (form) {
      test_container = form.down('.comment_test');
      test_container.value = "Please type 'moo'. Sorry, too much spam.";
      test_container.show();
    }
  }
});
